package session;

import java.util.ArrayList;

import org.apache.ibatis.session.SqlSession;

import bean.ClienteFinBean;
import bean.ClienteNoRegBean;
import persistence.ClienteFin;
import persistence.ClienteNoReg;
import util.SqlConnection;

/**
 * @since 2011.06.13
 * @author mhurtado
 *
 */


public class ClienteNoRegSession {
	
	private SqlSession  session;
	
	public ClienteNoRegSession(){
				
	}
	
	public ArrayList<ClienteNoRegBean> listarClienteNoReg() throws Exception {
		ArrayList<ClienteNoRegBean> list= null;
		try {			
			list = getSession().listarClienteNoReg();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return list;
	}

	
	public void guardarClienteNoReg(ClienteNoRegBean clienteNoRegBean) throws Exception {
		try {
			session = SqlConnection.getSessionFactory().openSession();			
			ClienteNoReg cliente = session.getMapper(ClienteNoReg.class);
			cliente.guardarClienteNoReg(clienteNoRegBean);
			session.commit();
		} catch (Exception e) {
			e.printStackTrace();
			session.rollback();
		} finally {
			session.close();
		}
		
	}

	
	public void editarClienteNoReg(ClienteNoRegBean clienteNoRegBean) throws Exception {
		try {
			session = SqlConnection.getSessionFactory().openSession();			
			ClienteNoReg cliente =session.getMapper(ClienteNoReg.class);
			cliente.editarClienteNoReg(clienteNoRegBean);
			session.commit();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		
	}

	
	public void eliminarClienteNoReg(Integer id)
			throws Exception {
		try {
			getSession().eliminarClienteNoReg(id);
			session.commit();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		
	}
	
	public void eliminarDirsClienteFin(Integer idCliente) throws Exception{
		try {
			
			getSession().eliminarClienteNoReg(idCliente);
			session.commit();
		} catch (Exception e) {
			session.rollback();
			e.printStackTrace();			
		} finally {
			session.close();
		}
		
	}

	
	public ClienteFinBean buscarIdClienteNoReg(String id) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	

	public ClienteNoReg getSession(){
		ClienteNoReg clienteNoReg=null;
		try {
			session = SqlConnection.getSessionFactory().openSession();			
			clienteNoReg= session.getMapper(ClienteNoReg.class);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return clienteNoReg;
	}

}
